package net.crazyadam.activity;

import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.RemoteException;
import android.view.View;
import android.widget.TextView;
import net.crazyadam.R;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Map;

/**
 * Copyright by <a href="http://crazyadam.net"><em><i>Joseph J.C. Tang</i></em></a> <br/>
 * Email: <a href="mailto:jinntrance@gmail.com">jinntrance@gmail.com</a>
 *
 * @author joseph
 * @version 1.0
 * @since 10/19/12 1:55 PM
 */
public class SenderActivity extends DopplerActivity {
    private TextView text;
    private AudioRecorder recorder;
    private Handler handler = new Handler();

    @Override
    public void doWhenAllReady() {
        text.setText("All Ready");
    }

    @Override
    protected void doAfterGetAllResults(Map<Integer, Float> resultList) {
        String result = "result ";
        for (Integer i : resultList.keySet()) {
            result += i + ":" + resultList.get(i) + ";\n";
        }
        text.setText(result);
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sender);
        text = (TextView) findViewById(R.id.text);
        text.setText("Created");
    }

    public void start(View view) {
        recorder = new AudioRecorder();
//        super.start();

        recorder.start();
        new Thread() {
            public void run() {
                while (recorder.receivable) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    String r = "";
                    for (int i : recorder.getCodes())
                        r += "--\n" + i;
                    final String result = r;
                    handler.post(
                            new Runnable() {
                                @Override
                                public void run() {
                                    updateView(result);
                                }
                            }
                    );
                }
            }
        }.start();
    }

    public void stop(View view) {
        super.stop();

       try {

          recorder.stopRecorder();
           File root;
            OutputStreamWriter out;
            if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                root = Environment.getExternalStorageDirectory();
            } else {
                root = Environment.getDataDirectory();
            }
            File file = new File(root, "audio" + System.currentTimeMillis() + ".txt");
            out = new OutputStreamWriter(new FileOutputStream(file.getAbsolutePath()));
            for (int i : recorder.getCodes())
                out.write(i + "\n");
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void updateView(String value) {
        if (null != text)
            text.setText(value);
    }
    public void login(View view){
        final int id1 = 16844515;
        final int id2 = 16844516; //这个是具体的用户ID，需要自己获取
        final int id3 = 16844517;
        final int id4 = 16844518;
         final String psw = "qw";
         final String mac1 = "77:88:99:AA:BB:CC";
         final String mac2 = "88:99:AA:BB:CC:DD";
        final String mac3 = "99:AA:BB:CC:DD:EE";
        final String mac4 = "AA:BB:CC:DD:EE:FF";
        new Thread(){

            @Override
            public void run() {
                try {
                    final int a=service.CCIUserLogin(id1,mac1,psw) ;
//                     final int a=service.CCIUserLogin(id2,mac2,psw);
//                    final int a=service.CCIUserLogin(id3,mac3,psw) ;
//                    final int a=service.CCIUserLogin(id4,mac4,psw) ;
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            if(1==a )
                                updateView("login successful");
                        }
                    }) ;
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}